package com.gosuncn.tools;

import java.sql.*;



/***
 *功能描述
 * @author zxl
 * @date 2021/7/26
 * @param
 * @return
*/
public class DbUtil {

    private static  final String URL = "jdbc:mysql://localhost:3306/db_FJ?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";
    private static  final String USERNAME = "root";
    private static  final String PASSWORD = "123456";
    private static  Connection conn = null;


    /***
     *将获得的数据库与java的链接返回（返回的类型为Connection）
     * @return 连接对象
    */
    public static  Connection getConnection(){
        try {
            //1.加载驱动程序;会将Driver类的实例注册到DriverManager类中
            Class.forName("com.mysql.cj.jdbc.Driver");
            //获得Connection对象，该对象就代表一个数据库的连接
            conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e){
            e.printStackTrace();
        }
        return conn;
    }  

    /***
     *关闭连接
     */
    public static void close(ResultSet rs, PreparedStatement prep,Connection conn){
        try {   //按后面顺序依次关闭
            if (rs !=null){
                rs.close();
            }
            if (prep !=null){
                prep.close();
            }
            if (conn !=null){
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }





}
